<template>
  <div class="container-page container-page-list">
    <el-pagination class="item-pagination" background :layout="paginationLayout" :current-page="currentPage" :page-size="pageSize" :page-sizes="pageSizes" :total="total" @current-change="handlePageChange" @size-change="handleSizeChange" />
    <el-table border :data="list" size="mini" :lazy="true">
      <el-table-column label="更多" type="expand" width="100">
        <template slot-scope="props" label-position="right" label-width="80px" size="mini">
          <el-form>
            <el-form-item v-if="props.row.tag" label="标签">
              <itemTags :value="props.row.tag" />
            </el-form-item>
            <el-form-item v-if="props.row.procucer" :label="props.row.type==5?'关键词':'出品'">
              <itemTags :value="props.row.procucer" />
            </el-form-item>
            <el-form-item v-if="props.row.href" label="链接">
              {{ props.row.href }}
            </el-form-item>
            <el-form-item :label="props.row.type==5?'封面':'海报'">
              <itemPosterList :value="props.row.poster" />
            </el-form-item>
            <el-form-item :label="props.row.type==5?'内容':'简介'">
              <el-card shadow="hover">
                <div v-html="props.row.description" />
              </el-card>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="标题" prop="title" header-align="center" />
      <el-table-column label="点击量" prop="hit" width="100" align="right" header-align="center" />
      <el-table-column label="添加时间" width="150" align="center">
        <template slot-scope="props">
          <span class="el-icon-date"> {{ new Date(props.row.created_at) | parseTime('{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150" align="center">
        <template slot-scope="props">
          <el-button size="mini" plain class="el-icon-edit" type="primary" @click="updateProject(props.row.id)" />
          <el-button size="mini" plain class="el-icon-delete" type="danger" @click="deleteProject(props.row.id)" />
        </template>
      </el-table-column>
    </el-table>
    <el-pagination class="item-pagination" background :layout="paginationLayout" :current-page="currentPage" :page-size="pageSize" :page-sizes="pageSizes" :total="total" @current-change="handlePageChange" @size-change="handleSizeChange" />
  </div>
</template>

<script>
// api
import { getProjectList, delProject } from '@/api/miva';
// item
import itemPosterList from '@/components/miva-item/item-poster-list';
import itemTags from './components/item-tags';
export default {
  components: { itemPosterList, itemTags },
  data() {
    return {
      list: [],
      total: 0,
      pageSize: 10,
      pageSizes: [5, 10, 15],
      currentPage: 1,
      paginationLayout: 'prev, pager, next,sizes'
    };
  },
  computed: {
    nowType() {
      return this.$route.meta.type;
    }
  },
  async mounted() {
    await this.getProjectList();
  },
  methods: {
    // 切换页面
    async handlePageChange(nowPage) {
      this.currentPage = nowPage;
      await this.getProjectList();
    },
    async handleSizeChange(nowSize) {
      this.pageSize = nowSize;
      await this.getProjectList();
    },
    // 获取项目列表
    async getProjectList() {
      const res = await getProjectList({
        type: this.nowType,
        page: this.currentPage,
        size: this.pageSize
      });
      this.list = res.data;
      this.total = res.total;
    },
    // 修改项目
    async updateProject(id) {
      const obj = {
        1: 'html5',
        2: 'longPic',
        3: 'poster',
        4: 'video',
        5: 'zhuanti'
      };
      this.$router.push({
        path: `/${obj[this.nowType]}/update`,
        query: { id }
      });
    },
    // 删除项目
    async deleteProject(id) {
      this.$confirm('确认删除嘛?', {
        confirmButtonText: '确认',
        cancelButtonText: '取消'
      })
        .then(async() => {
          await delProject({ id });
          await this.getProjectList();
        })
        .catch(() => {});
    }
  }
};
</script>

<style lang="scss" scoped>
.container-page-list {
  .item {
    &-pagination {
      margin: 30px 0;
    }
  }
}
</style>
